# matrix_ops.py - 矩阵运算模块

# 控制 import * 导入的内容
__all__ = ['add', 'multiply']

def add(matrix1, matrix2):
    """矩阵加法运算
    Args:
        matrix1 (list): 第一个矩阵
        matrix2 (list): 第二个矩阵
    Returns:
        list: 矩阵之和
    """
    # 简化的矩阵加法实现
    if len(matrix1) != len(matrix2) or len(matrix1[0]) != len(matrix2[0]):
        raise ValueError("矩阵维度不匹配")
    
    result = []
    for i in range(len(matrix1)):
        row = []
        for j in range(len(matrix1[0])):
            row.append(matrix1[i][j] + matrix2[i][j])
        result.append(row)
    return result

def multiply(matrix1, matrix2):
    """矩阵乘法运算
    Args:
        matrix1 (list): 第一个矩阵
        matrix2 (list): 第二个矩阵
    Returns:
        list: 矩阵之积
    """
    # 简化的矩阵乘法实现
    if len(matrix1[0]) != len(matrix2):
        raise ValueError("矩阵维度不匹配")
    
    result = [[0 for _ in range(len(matrix2[0]))] for _ in range(len(matrix1))]
    for i in range(len(matrix1)):
        for j in range(len(matrix2[0])):
            for k in range(len(matrix2)):
                result[i][j] += matrix1[i][k] * matrix2[k][j]
    return result

# 矩阵运算模块测试代码
if __name__ == "__main__":
    print("矩阵运算模块测试:")
    m1 = [[1, 2], [3, 4]]
    m2 = [[5, 6], [7, 8]]
    
    result_add = add(m1, m2)
    print(f"矩阵加法: {m1} + {m2} = {result_add}")
    
    result_mul = multiply(m1, m2)
    print(f"矩阵乘法: {m1} * {m2} = {result_mul}")